﻿Public Class Login

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.

    Private MODULE_OWNER As New Dictionary(Of Integer, String) '' Key=ModuleID, 

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        '' Việt: Viết code kiểm tra user, pass
        Dim sqlWhere As String
        sqlWhere = "" & I_Users.ColumnName_UserName & " = '" & Me.txtUserName.Text & "'"
        Dim lstUser As List(Of PO) = New List(Of PO)
        Dim x_user As X_Users = New X_Users(-1)
        lstUser = New Query(x_user, sqlWhere).list
        If (lstUser Is Nothing) Then
            MessageBox.Show("UserName is not correct!")
            Return
        End If

        x_user = lstUser.Item(0)
        If (Not x_user.getValue(I_Users.ColumnName_Password).Equals(txtPassword.Text)) Then
            MessageBox.Show("Password is not correct!")
            Return
        End If

        Dim GroupID As Integer = x_user.getValue(I_Users.ColumnName_GroupID)


        sqlWhere = "" & I_ModuleAndGroup.ColumnName_GroupID & " = " & GroupID & ""

        Dim daMAG As DataTable = New DataTable
        daMAG = New Query(New X_ModuleAndGroup(-1), sqlWhere).dataTable


        If (daMAG Is Nothing) Then
            MessageBox.Show("User have no permission in application!")
            Return
        End If

        Dim i As Integer = 0
        For i = 0 To daMAG.Rows.Count - 1
            Me.MODULE_OWNER.Add(daMAG.Rows(i)(I_ModuleAndGroup.ColumnName_ModuleID), daMAG.Rows(i)(I_ModuleAndGroup.ColumnName_Permission))
        Next
        Dim faManagament As FAManagement = New FAManagement(Me.MODULE_OWNER)
        faManagament.Visible = True
        Me.Hide()
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub Login_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '' Load POInfo cho tất các các table
        PO.iniApp()
    End Sub
End Class
